use std::fs;

#[tauri::command]
pub fn save_text_file(filename: String, content: String) -> Result<(), String> {
    let config_dir = dirs::config_dir().ok_or("无法获取配置目录")?;
    let dir = config_dir.join(env!("CARGO_PKG_NAME"));

    // 确保目录存在
    fs::create_dir_all(&dir).map_err(|e| format!("创建目录失败: {}", e))?;

    let file_path = dir.join(filename);
    fs::write(file_path, content).map_err(|e| format!("写入文件失败: {}", e))
}

#[tauri::command]
pub fn read_text_file(filename: String) -> Result<String, String> {
    let config_dir = dirs::config_dir().ok_or("无法获取配置目录")?;
    let dir = config_dir.join(env!("CARGO_PKG_NAME"));
    let file_path = dir.join(filename);

    fs::read_to_string(file_path).map_err(|e| format!("读取文件失败: {}", e))
}
